<?php
namespace app\api\service;
use think\Controller;
use think\Db;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/26 0026
 * Time: 15:40
 */

class Shop extends Controller {

    /**
     * 获取店铺首页数据
     * @param $param
     * @return array
     */
    public static function get_shop_index($param) {
        try {
            if(isset($param['member_id'])) {
                $where['member_id'] = $param['member_id'];
            } else if(isset($param['id'])) {
                $where['id'] = $param['id'];
            } else {
                throw new \Exception('参数错误');
            }
            $shop = model('shop')->get($where);
            $data = ['status' => 0];
            if ($shop) {
                $image = $shop->images()->where('is_main', 1)->find();
                $data = [
                    'id' => $shop->id,
                    'name' => $shop->name,
                    'member_id' => $shop->member_id,
                    'shopkeeper' => $shop->shopkeeper,
                    'logo' => get_img_url($shop->logo),
                    'status' => $shop->status,
                    'level' => $shop->commentLevel() > 0 ? $shop->commentLevel() : 5,
                    'image' => get_img_url($image ? $image->image : ''),
                ];
            }
            return api_return(200, $data);
        } catch (\Exception $e) {
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 获取店铺详情
     * @param $param
     * @return array
     */
    public static function get_shop_info($param) {
        try {

            if (isset($param['status']) && $param['status'] > 0) {
                $where['status'] = $param['status'];
            }

            if(isset($param['member_id'])) {
                $where['member_id'] = $param['member_id'];
            } else if(isset($param['id'])) {
                $where['id'] = $param['id'];
            } else {
                throw new \Exception('参数错误');
            }
            $shop = model('shop')->get($where);


            if ($shop) {
                $image = $shop->images ? $shop->images()->where('is_main', 1)->find() : '';

                $comment_num = model('comment')->where('shop_id', $shop->id)->count();
//
                $category = model('shop_category')->where('shop_id', $shop->id)->column('category_id');

                // 获取所有分类
                $category_all = model('category')->all(['delete' => 0, 'status' => 1]);
                $shop_category = [];
                foreach ($category as $cate) {
                    foreach ($category_all as $c) {
                        if ($cate == $c->id) {
                            $shop_category[] = $c;
                        }
                    }
                }

                $level = $shop->commentLevel() > 0 ? $shop->commentLevel() : 5;
//
                $order_num = model('order')->where('shop_id', $shop->id)->where('status', 'gt', 1)->where('status', 'neq', 7)->count();

                $data = [
                    'id' => $shop->id,
                    'name' => $shop->name,
                    'member_id' => $shop->member_id,
                    'shopkeeper' => $shop->shopkeeper,
                    'mobile' => $shop->mobile,
                    'logo' => get_img_url($shop->logo),
                    'address' => $shop->address,
                    'lat' => $shop->lat,
                    'lng' => $shop->lng,
                    'business_license_number' => $shop->business_license_number,
                    'business_license_image' => get_img_url($shop->business_license_image),
                    'business_certificate' => get_img_url($shop->business_certificate),
                    'status' => $shop->status,
                    'audit_time' => $shop->audit_time,
                    'audit_msg' => $shop->audit_msg,
                    'comment_num' => $comment_num,
                    'category' => $shop_category,
                    'cate_ids' => $category,

                    'level' => $level,
                    'order_num' => $order_num,
                    'image' => get_img_url($image ? $image->image : ''),
                ];

                // 商品评价信息
                $comment = $shop->comments()->limit(2)->select();
                $comment_data= [];
                foreach ($comment as $k => $v) {
                    $images = [];
                    $v->img_1 ? $images[] = get_img_url($v->img_1) : $a = '';
                    $v->img_2 ? $images[] = get_img_url($v->img_2) : $a = '';
                    $v->img_3 ? $images[] = get_img_url($v->img_3) : $a = '';
                    $v->img_4 ? $images[] = get_img_url($v->img_4) : $a = '';
                    $v->img_5 ? $images[] = get_img_url($v->img_5) : $a = '';

                    $comment_data[] = [
                        'headimgurl' => get_img_url($v->member->memberRoleInfo->headimgurl),
                        'nickname' => $v->member->memberRoleInfo->nickname,
                        'level' => $v->level,
                        'create_time' => $v->create_time,
                        'msg' => $v->content,
                        'images' => $images,
                        'product_name' => $v->product->name,
                    ];
                }
                $data['comment'] = $comment_data;

                $data['category_all'] = $category_all;
            } else {
                $data = ['status' => 0];
            }
            return api_return(200, $data);
        } catch (\Exception $e) {
            return api_return(0, [], $e->getMessage());
        }
    }

    /**
     * 店铺申请
     * @param $param
     * @return array
     */
    public static function save_shop($param) {
        Db::startTrans();
        try {
            // 如果ID == 0 表示是首次申请   检查用户是否已申请店铺
            if ($param['id'] == 0) {
                $shop = model('shop')->get(['member_id' => $param['member_id']]);
                if ($shop) {
                    throw new \Exception('请勿重复申请！');
                }
            }

            // 组装店铺数据
            $data = [
                'name' => $param['name'],
                'shopkeeper' => $param['shopkeeper'],
                'mobile' => $param['mobile'],
                'logo' => $param['logo'] . '-100',
                'address' => $param['address'],
                'lat' => $param['lat'],
                'lng' => $param['lng'],
                'status' => 1,
                'business_license_number' => $param['business_license_number'],
                'business_license_image' => $param['business_license_image'] . '-400',
                'business_certificate' => $param['business_certificate'] . '-400'
            ];

            $model = model('shop');
            if ($param['id'] == 0) {
                $data['member_id'] = $param['member_id'];
                $model->save($data);
            } else {
                $model->save($data, ['id' => $param['id'], 'member_id' => $param['member_id']]);
            }

            Db::commit();
            return api_return(200, []);
        } catch (\Exception $e) {
            Db::rollback();
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 编辑店铺经营分类
     * @param $param
     * @return array
     */
    public static function edit_shop_category($param) {
        Db::startTrans();
        try {
            $shop = model('shop')->get(['member_id' => $param['member_id'], 'id' => $param['shop_id'], 'status' => 2]);
            if (!$shop) {
                throw new \Exception('店铺不存在！');
            }

            if (!isset($param['categorys']) || empty($param['categorys'])) {
                // 删除之前的数据
                model('shop_category')->destroy(['shop_id' => $param['shop_id']]);
            } else {
                if (!is_array($param['categorys'])) {
                    throw new \Exception('数据格式错误！');
                }

                if (!empty($param['categorys'])) {
                    // 删除之前的数据
                    model('shop_category')->destroy(['shop_id' => $param['shop_id']]);

                    $data = [];
                    foreach ($param['categorys'] as $v) {
                        $data[] = [
                            'shop_id' => $param['shop_id'],
                            'category_id' => $v
                        ];
                    }

                    model('shop_category')->saveAll($data);
                }
            }

            Db::commit();
            return api_return(200, []);
        } catch (\Exception $e) {
            Db::rollback();
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 获取店铺图片
     * @param $param
     * @return array
     */
    public static function get_shop_image($param) {
        try {
            $shop = model('shop')->get(['member_id' => $param['member_id'], 'id' => $param['id'], 'status' => 2]);
            if (!$shop) {
                throw new \Exception('店铺不存在！');
            }

            $images = model('shop_image')->all(['shop_id' => $param['id']]);
            $data = [];
            foreach ($images as $v) {
                $data[] = [
                    'id' => $v->id,
                    'image' => $v->image,
                    'thumb' => $v->thumb,
                    'is_main' => $v->is_main,
                ];
            }

            Db::commit();
            return api_return(200, $data);
        } catch (\Exception $e) {
            Db::rollback();
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 添加店铺图片
     * @param $param
     * @return array
     */
    public static function add_shop_image($param) {
        Db::startTrans();
        try {
            $shop = model('shop')->get(['member_id' => $param['member_id'], 'id' => $param['id'], 'status' => 2]);
            if (!$shop) {
                throw new \Exception('店铺不存在！');
            }

            $data = [
                'shop_id' => $param['id'],
                'image' => $param['path'] . '-700',
                'thumb' => $param['path'] . '-300',
            ];

            $count = model('shop_image')->where('shop_id', $param['id'])->count();
            if (!$count) {
                $data['is_main'] = 1;
            }

            model('shop_image')->save($data);

            Db::commit();
            return api_return(200, []);
        } catch (\Exception $e) {
            Db::rollback();
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 设置店铺主图
     * @param $param
     * @return array
     */
    public static function save_main_image($param) {
        Db::startTrans();
        try {
            $shop = model('shop')->get(['member_id' => $param['member_id'], 'id' => $param['shop_id'], 'status' => 2]);
            if (!$shop) {
                throw new \Exception('店铺不存在！');
            }

            model('shop_image')->save(['is_main' => 0], ['shop_id' => $param['shop_id']]);

            model('shop_image')->save(['is_main' => 1], ['id' => $param['id'], 'shop_id' => $param['shop_id']]);

            Db::commit();
            return api_return(200, []);
        } catch (\Exception $e) {
            Db::rollback();
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 删除店铺图片
     * @param $param
     * @return array
     */
    public static function delete_shop_image($param) {
        Db::startTrans();
        try {
            $shop = model('shop')->get(['member_id' => $param['member_id'], 'id' => $param['shop_id'], 'status' => 2]);
            if (!$shop) {
                throw new \Exception('店铺不存在！');
            }

            $image = model('shop_image')->where(['id' => $param['id'], 'shop_id' => $param['shop_id']])->find();
            if (!$image) {
                throw new \Exception('图片不存在！');
            }
            if ($image->is_main == 1) {
                throw new \Exception('封面图片不能删除！');
            }

            model('shop_image')->where(['id' => $param['id'], 'shop_id' => $param['shop_id']])->delete();

            Db::commit();
            return api_return(200, []);
        } catch (\Exception $e) {
            Db::rollback();
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 获取店铺经营分类
     * @param $param
     * @return array
     */
    public static function get_shop_category($param) {
        Db::startTrans();
        try {
            $shop = model('shop')->get(['id' => $param['id'], 'status' => 2]);
            if (!$shop) {
                throw new \Exception('店铺不存在！');
            }
            $category = model('shop_category')->all(['shop_id' => $param['id']]);
            $data= [];
            foreach ($category as $v) {
                $data[] = [
                    'name' => $v->category->name,
                    'id' => $v->category_id
                ];
            }
            Db::commit();
            return api_return(200, $data);
        } catch (\Exception $e) {
            Db::rollback();
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 获取店铺补货记录
     * @param $param
     * @return array
     */
    public static function get_replenishment_list($param) {
        try {
            $shop = model('shop')->get(['member_id' => $param['member_id'], 'id' => $param['id'], 'status' => 2]);
            if (!$shop) {
                throw new \Exception('店铺不存在！');
            }
            $list = model('shop_replenishment_apply_for')->where(['shop_id' => $param['id']])->order('id desc')->paginate();
            $data= [];
            if ($list) {
                foreach ($list as $k => $v) {
                    $data['list'][] = [
                        'id' => $v->id,
                        'create_time' => $v->create_time,
                        'status' => $v->status,
                        'status_text' => get_replenishment_status($v->status),
                        'msg' => $v->msg,
                        'num' => $v->num,
                        'product_thumb' => get_img_url($v->product->thumb),
                        'product_name' => $v->product->name
                    ];
                }
                $list = $list->toArray();
                $is_more = $list['total'] - ($list['current_page'] * $list['per_page']);
                $data['is_more'] = $is_more;
            }

            return api_return(200, $data);
        } catch (\Exception $e) {
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 店铺补货确认收货
     * @param $param
     * @return array
     */
    public static function replenishment_confirm_receipt($param) {
        Db::startTrans();
        try {
            $shop = model('shop')->get(['member_id' => $param['member_id'], 'id' => $param['shop_id'], 'status' => 2]);
            if (!$shop) {
                throw new \Exception('店铺不存在！');
            }
            $replenishment = model('shop_replenishment_apply_for')->get(['id' => $param['id'], 'shop_id' => $param['shop_id']]);
            if (!$replenishment) {
                throw new \Exception('记录不存在!');
            }

            if ($replenishment->status != 2) {
                throw new \Exception('状态异常!');
            }

            // 修改补货记录状态
            model('shop_replenishment_apply_for')->save(['status' => 3], ['id' => $param['id'], 'shop_id' => $param['shop_id']]);

            // 修改上级库存
            $shop_product = model('shop_product')->get(['shop_id' => $replenishment->shop_id, 'product_id' => $replenishment->product_id]);
            if ($shop_product) {
                model('shop_product')->where(['shop_id' => $replenishment->shop_id, 'product_id' => $replenishment->product_id])->setInc('inventory', $replenishment->num);
            } else {
                model('shop_product')->save(['shop_id' => $replenishment->shop_id, 'product_id' => $replenishment->product_id, 'inventory' => $replenishment->num]);
            }

            // 修改商品二维码状态
            model('product_code')->save(['status' => 2], ['apply_for_id' => $param['id']]);

            Db::commit();
            return api_return(200, []);
        } catch (\Exception $e) {
            Db::rollback();
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 店铺补货取消申请
     * @param $param
     * @return array
     */
    public static function replenishment_cancel($param) {
        try {
            $shop = model('shop')->get(['member_id' => $param['member_id'], 'id' => $param['shop_id'], 'status' => 2]);
            if (!$shop) {
                throw new \Exception('店铺不存在！');
            }
            $replenishment = model('shop_replenishment_apply_for')->get(['id' => $param['id'], 'shop_id' => $param['shop_id']]);
            if (!$replenishment) {
                throw new \Exception('记录不存在!');
            }

            if ($replenishment->status != 1) {
                throw new \Exception('状态异常!');
            }

            model('shop_replenishment_apply_for')->save(['status' => 4], ['id' => $param['id'], 'shop_id' => $param['shop_id']]);

            Db::commit();
            return api_return(200, []);
        } catch (\Exception $e) {
            Db::rollback();
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 获取店铺评论列表
     * @param $param
     * @return array
     */
    public static function get_shop_comment_list($param) {
        try {
            $where['shop_id'] = $param['shop_id'];
            if(isset($param['tag_type']) && $param['tag_type'] == 2) {
                $where['img_1'] = ['neq', ''];
            }

            $shop_comment = model('comment')->where($where)->order('id desc')->paginate();

            $data= [];
            foreach ($shop_comment as $k => $v) {
                $images = [];
                $v->img_1 ? $images[] = get_img_url($v->img_1) : $a = '';
                $v->img_2 ? $images[] = get_img_url($v->img_2) : $a = '';
                $v->img_3 ? $images[] = get_img_url($v->img_3) : $a = '';
                $v->img_4 ? $images[] = get_img_url($v->img_4) : $a = '';
                $v->img_5 ? $images[] = get_img_url($v->img_5) : $a = '';

                $data['list'][] = [
                    'headimgurl' => get_img_url($v->member->memberRoleInfo->headimgurl),
                    'nickname' => $v->member->memberRoleInfo->nickname,
                    'level' => $v->level,
                    'create_time' => $v->create_time,
                    'msg' => $v->content,
                    'images' => $images,
                    'product_name' => $v->product->name,
                ];
            }
            $list = $shop_comment->toArray();
            $is_more = $list['total'] - ($list['current_page'] * $list['per_page']);

            $data['is_more'] = $is_more;

            return api_return(200, $data);
        } catch (\Exception $e) {
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 获取店铺评论总数
     * @param $param
     * @return array
     */
    public static function get_shop_comment_count($param) {
        try {
            // 店铺总评论
//            $where = "shop_id = $param[shop_id]";
            $where['shop_id'] = $param['shop_id'];
            $count = model('comment')->where($where)->count();

            // 店铺带图片评论总数
            $where['img_1'] = ['neq', ''];
            $img_count = model('comment')->where($where)->count();

            $data = [
                'count' => $count,
                'img_count' => $img_count
            ];

            return api_return(200, $data);
        } catch (\Exception $e) {
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 获取待抢订单列表
     * @param $param
     * @return array
     */
    public static function get_vie_for_order_list($param) {
        try {
            $shop = model('shop')->get(['member_id' => $param['member_id'], 'id' => $param['id'], 'status' => 2]);
            if (!$shop) {
                throw new \Exception('店铺不存在！');
            }

            // 根据用户经纬度 获取附近活动
            $coord = getAround($shop->lat, $shop->lng, 10000);
            $where['lat'] = ['between', [$coord[0], $coord[1]]];
            $where['lng'] = ['between', [$coord[2], $coord[3]]];
            $where['status'] = 1;
            $where['type'] = 1;
            $order_list = model('order')->where($where)->order('id desc')->select();
            $data = [];
            foreach ($order_list as $v) {
                $data[] = [
                    'id' => $v->id,
                    'headimgurl' => get_img_url($v->orderMember->memberRoleInfo->headimgurl),
                    'nickname' => $v->orderMember->memberRoleInfo->nickname,
                    'create_time' => $v->create_time,
                    'msg' => $v->msg,
                    'drink_time' => $v->drink_time,
                    'address' => $v->address
                ];
            }

            return api_return(200, $data);
        } catch (\Exception $e) {
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 获取待抢订单详情
     * @param $param
     * @return array
     */
    public static function get_vie_for_order_info($param) {
        try {
            $shop = model('shop')->get(['member_id' => $param['member_id'], 'id' => $param['shop_id'], 'status' => 2]);
            if (!$shop) {
                throw new \Exception('店铺不存在！');
            }

            $order = model('order')->get(['id' => $param['id']]);
            if ($order->status != 1) {
                throw new \Exception('订单已被抢！');
            }
            $data = [
                'id' => $order->id,
                'headimgurl' => get_img_url($order->orderMember->memberRoleInfo->headimgurl),
                'nickname' => $order->orderMember->memberRoleInfo->nickname,
                'create_time' => $order->create_time,
                'msg' => $order->msg,
                'order_sn' => $order->order_sn,
                'consignee' => $order->consignee,
                'consignment_type' => $order->consignment_type,
                'address' => $order->address,
                'mobile' => $order->orderMember->username
            ];
            return api_return(200, $data);
        } catch (\Exception $e) {
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 抢单
     * @param $param
     * @return array
     */
    public static function vie_for_order($param) {
        try {
            $shop = model('shop')->get(['member_id' => $param['member_id'], 'id' => $param['shop_id'], 'status' => 2]);
            if (!$shop) {
                throw new \Exception('店铺不存在！');
            }
            $order = model('order')->get(['id' => $param['id']]);
            if ($order->type != 1) {
                throw new \Exception('参数错误!');
            }
            if ($order->status != 1) {
                throw new \Exception('订单已被抢!');
            }
            $data = [
                'status' => 2,
                'shop_id' => $param['shop_id'],
                'receiving_time' => time()
            ];
            model('order')->save($data, ['id' => $param['id']]);
            return api_return(200, []);
        } catch (\Exception $e) {
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 获取店铺订单列表
     * @param $param
     * @return array
     */
    public static function get_shop_order_list($param) {
        try {
            $shop = model('shop')->get(['member_id' => $param['member_id'], 'id' => $param['id'], 'status' => 2]);
            if (!$shop) {
                throw new \Exception('店铺不存在！');
            }

            $list = model('order')->where(['shop_id' => $param['id'], 'type' => $param['type']])->order('id desc')->paginate();

            $data = [];
            if ($list) {
                $data['list'] = [];
                foreach ($list as $k => $v) {
                    $order_product = model('order_product')->where('order_sn', $v->order_sn)->select();
                    $product = [];
                    $product_num = 0;
                    foreach ($order_product as $p) {
                        $product_num += $p->num;
                        $product[] = [
                            'name' => $p->name,
                            'image' => $p->image,
                            'price' => $p->price,
                            'num' => $p->num,
                        ];
                    }
                    $data['list'][] = [
                        'id' => $v->id,
                        'create_time' => $v->create_time,
                        'status' => $v->status,
                        'status_text' => get_order_status($v->status, $v->type),
                        'price' => $v->price,
                        'type' => $v->type,
                        'msg' => $v->msg,
                        'drink_time' => $v->drink_time,
                        'address' => $v->address,
                        'products' => $product,
                        'products_num' => $product_num,
                        'headimgurl' => get_img_url($v->orderMember->memberRoleInfo->headimgurl),
                        'nickname' => $v->orderMember->memberRoleInfo->nickname
                    ];
                }
                $list = $list->toArray();
                $is_more = $list['total'] - ($list['current_page'] * $list['per_page']);
                $data['is_more'] = $is_more;
            }
            return api_return(200, $data);
        } catch (\Exception $e) {
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 保存添加到订单的产品
     * @param $param
     * @return array
     */
    public static function save_shop_order_product($param) {
        Db::startTrans();
        try {
            // 检查店铺是否存在
            $shop = model('shop')->get(['member_id' => $param['member_id'], 'id' => $param['shop_id'], 'status' => 2]);
            if (!$shop) {
                throw new \Exception('店铺不存在！');
            }

            // 检查店铺产品库存
            $shop_product = model('shop_product')->get(['shop_id' => $param['shop_id'], 'product_id' => $param['id']]);
            if (!$shop_product || !$shop_product->inventory) {
                throw new \Exception('产品库存不足！');
            }

            // 获取订单信息
            $order = model('order')->get(['id' => $param['order_id'], 'type' => 1, 'status' => 2, 'shop_id' => $param['shop_id']]);
            if (!$order) {
                throw new \Exception('订单异常！');
            }

            // 检查商品是否已添加
            $order_product = model('order_product')->get(['order_sn' => $order->order_sn, 'product_id' => $shop_product->product_id]);
            if ($order_product) {
                throw new \Exception('该商品已加入订单！');
            }

            // 添加产品到订单
            $order_product_info = [
                'order_sn' => $order->order_sn,
                'product_id' => $shop_product->product_id,
                'name' => $shop_product->product->name,
                'image' => get_img_url($shop_product->product->thumb),
                'num' => 1,
                'price' => $shop_product->product->price
            ];
            model('order_product')->save($order_product_info);

            // 减库存
            model('shop_product')->where(['shop_id' => $param['shop_id'], 'product_id' => $param['id']])->setDec('inventory');

            Db::commit();
            return api_return(200, []);
        } catch (\Exception $e) {
            Db::rollback();
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 获取店铺订单详情
     * @param $param
     * @return array
     */
    public static function get_shop_order_info($param) {
        try {
            $shop = model('shop')->get(['member_id' => $param['member_id'], 'id' => $param['shop_id'], 'status' => 2]);
            if (!$shop) {
                throw new \Exception('店铺不存在！');
            }

            $order = model('order')->get(['id' => $param['id'], 'shop_id' => $param['shop_id']]);
            if (!$order) {
                throw new \Exception('订单不存在！');
            }

            $order_product = model('order_product')->where('order_sn', $order->order_sn)->select();
            $product = [];
            $product_num = 0;
            $total_price = 0;
            foreach ($order_product as $p) {
                $product_num += $p->num;
                $total_price += $p->price * $p->num;
                $product[] = [
                    'id' => $p->id,
                    'name' => $p->name,
                    'image' => $p->image,
                    'price' => number_format($p->price, 2),
                    'num' => $p->num,
                ];
            }
            $data = [
                'order_sn' => $order->order_sn,
                'id' => $order->id,
                'create_time' => $order->create_time,
                'status' => $order->status,
                'status_text' => get_order_status($order->status, $order->type),
                'price' => number_format($order->price, 2),
                'consignment_type' => $order->consignment_type,
                'consignee' => $order->consignee,
                'mobile' => $order->mobile,
                'type' => $order->type,
                'msg' => $order->msg,
                'drink_time' => $order->drink_time,
                'address' => $order->address,
                'products' => $product,
                'products_num' => $product_num,
                'total_price' => number_format($total_price, 2),
                'headimgurl' => get_img_url($order->orderMember->memberRoleInfo->headimgurl),
                'nickname' => $order->orderMember->memberRoleInfo->nickname
            ];

            return api_return(200, $data);
        } catch (\Exception $e) {
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 店铺调整订单产品数量 必须是需求单且状态为2的时候才可以修改
     * @param $param
     * @return array
     */
    public static function edit_order_product_num($param) {
        Db::startTrans();
        try {
            // 检查店铺是否存在
            $shop = model('shop')->get(['member_id' => $param['member_id'], 'id' => $param['shop_id'], 'status' => 2]);
            if (!$shop) {
                throw new \Exception('店铺不存在！');
            }

            // 获取订单信息
            $order = model('order')->get(['id' => $param['order_id'], 'type' => 1, 'status' => 2, 'shop_id' => $param['shop_id']]);
            if (!$order) {
                throw new \Exception('订单异常！');
            }

            // 获取订单产品信息
            $order_product = model('order_product')->get(['id' => $param['id'], 'order_sn' => $order->order_sn]);
            if (!$order_product) {
                throw new \Exception('产品不存在！');
            }

            if ($param['num'] > $order_product->num) {
                $num = $param['num'] - $order_product->num;
                // 检查店铺产品库存
                $shop_product = model('shop_product')->get(['shop_id' => $param['shop_id'], 'product_id' => $order_product->product_id]);
                if (!$shop_product || $shop_product->inventory < $num) {
                    throw new \Exception('产品库存不足！');
                }
            }

            if ($param['num'] == 0) {
                // 如果为0   则删除商品
                model('order_product')->where(['id' => $param['id'], 'order_sn' => $order->order_sn])->delete();
            } else {
                // 修改订单商品数量库存
                model('order_product')->save(['num' => $param['num']], ['id' => $param['id'], 'order_sn' => $order->order_sn]);
            }

            //  修改店铺产品库存
            if ($param['num'] > $order_product->num) {
                // 如果传进来的数量大于之前的数量
                //  表示在增加订单商品数量
                //  对应的  店铺商品库存就是减少
                $num = $param['num'] - $order_product->num;
                // 减库存
                model('shop_product')->where(['shop_id' => $param['shop_id'], 'product_id' => $order_product->product_id])->setDec('inventory', $num);
            } else {
                $num = $order_product->num - $param['num'];
                // 加库存
                model('shop_product')->where(['shop_id' => $param['shop_id'], 'product_id' => $order_product->product_id])->setInc('inventory', $num);
            }

            Db::commit();
            return api_return(200, []);
        } catch (\Exception $e) {
            Db::rollback();
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 删除订单商品
     * @param $param
     * @return array
     */
    public static function delete_order_product($param) {
        Db::startTrans();
        try {
            // 检查店铺是否存在
            $shop = model('shop')->get(['member_id' => $param['member_id'], 'id' => $param['shop_id'], 'status' => 2]);
            if (!$shop) {
                throw new \Exception('店铺不存在！');
            }

            // 获取订单信息
            $order = model('order')->get(['id' => $param['order_id'], 'type' => 1, 'status' => 2, 'shop_id' => $param['shop_id']]);
            if (!$order) {
                throw new \Exception('订单异常！');
            }

            // 获取订单产品信息
            $order_product = model('order_product')->get(['id' => $param['id'], 'order_sn' => $order->order_sn]);
            if (!$order_product) {
                throw new \Exception('产品不存在！');
            }

            // 删除店铺产品
            model('order_product')->where(['id' => $param['id'], 'order_sn' => $order->order_sn])->delete();

            // 商品删除，回复产品库存
            model('shop_product')->where(['shop_id' => $param['shop_id'], 'product_id' => $order_product->product_id])->setInc('inventory', $order_product->num);

            Db::commit();
            return api_return(200, []);
        } catch (\Exception $e) {
            Db::rollback();
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 推送订单
     * @param $param
     * @return array
     */
    public static function push_order($param) {
        Db::startTrans();
        try {
            // 检查店铺是否存在
            $shop = model('shop')->get(['member_id' => $param['member_id'], 'id' => $param['shop_id'], 'status' => 2]);
            if (!$shop) {
                throw new \Exception('店铺不存在！');
            }

            // 获取订单信息
            $order = model('order')->get(['id' => $param['id'], 'type' => 1, 'status' => 2, 'shop_id' => $param['shop_id']]);
            if (!$order) {
                throw new \Exception('订单异常！');
            }

            // 获取订单产品信息
            $order_product = model('order_product')->all(['order_sn' => $order->order_sn]);
            if (!$order_product) {
                throw new \Exception('请先添加产品！');
            }

            $order_price = 0;
            foreach ($order_product as $v) {
                $order_price += $v->num * $v->price;
            }

            $order_data = [
                'price' => $order_price,
                'status' => 3,
                'stocking_time' => time()
            ];

            model('order')->save($order_data, ['id' => $order->id]);

            Db::commit();
            return api_return(200, []);
        } catch (\Exception $e) {
            Db::rollback();
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 修改店铺信息
     * @param $param
     * @return bool
     * @throws \Exception
     */
    public static function save_shop_info($param) {
        // 检查店铺是否存在
        $shop = model('shop')->get(['member_id' => $param['member_id'], 'id' => $param['shop_id'], 'status' => 2]);
        if (!$shop) {
            throw new \Exception('店铺不存在！');
        }

        if ($param['field'] == 'logo') {
            $param['value'] .= '-100';
        }

        if ($param['field'] == 'business_license_image' || $param['field'] == 'business_certificate') {
            $param['value'] .= '-400';
        }

        $shop->save([$param['field'] => $param['value']]);

        return api_return(200, []);
    }
}